﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.HumanResources;
using BusinessObjects.HumanResources;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer.HumanResources
{
    public class SqlServerDepartmentDao : IDepartmentDao
    {
        public List<Department> GetDepartments() { return null; }
        public List<Department> GetDepartmentsByBranch(string restaurantId)
        {
            string procedures = "Get_All_Departments_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantId = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public List<Department> GetDepartmentsActiveByBranch(bool isActive, string restaurantId)
        {
            string procedures = "Get_All_Departments_ByCondition";
            int _isActive = 0;

            if (isActive == true) { _isActive = 1; }
            else { _isActive = 0; }


            object[] parms = { "@WhereClause", " WHERE RestaurantId = '" + restaurantId + "' AND IsActive = " + _isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public int GetCountDepartmentByParentId(string parentId)
        {
            string procedures = "Get_All_Departments_ByCondition";

            object[] parms = { "@WhereClause", " WHERE ParentDepartmentID = '" + parentId + "' " };
            var inventoryItems = Db.ReadList(procedures, true, Make, parms);
            if (inventoryItems != null) { return inventoryItems.Count; }
            else { return 0; }
        }
        public Department GetDepartment(string id)
        {
            string procedures = "Get_All_Departments_ByCondition";

            object[] parms = { "@WhereClause", " WHERE DepartmentID = '" + id + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public void InsertDepartment(Department department)
        {
            string sql = "Insert_Department";

            department.DepartmentID = Db.Insert(sql, true, Take(department));
        }

        public void UpdateDepartment(Department department)
        {
            string sql = "Update_Department";

            Db.Update(sql, true, Take(department));
        }

        public void DeleteDepartment(Department department)
        {
            string sql = "Delete_Department";

            Db.Update(sql, true, Take(department.DepartmentID));
        }

        private object[] Take(string departmentId)
        {
            return new object[]  
            {
                "@DepartmentID", departmentId,
            };
        }

        private object[] Take(Department department)
        {
            return new object[]  
            {
                "@DepartmentID", department.DepartmentID,
                "@DepartmentCode", department.DepartmentCode,
                "@DepartmentName", department.DepartmentName,
                "@ParentDepartmentID", department.ParentDepartmentID,
                "@IsParent", department.IsParent,
			    "@RestaurantId", department.RestaurantId,
                "@Description", department.Description ,
                "@IsActive", department.IsActive
            };
        }

        private static Func<IDataReader, Department> Make = reader =>
            new Department
            {
                DepartmentID = reader["DepartmentID"].AsString(),
                DepartmentCode = reader["DepartmentCode"].AsString(),
                DepartmentName = reader["DepartmentName"].AsString(),
                ParentDepartmentID = reader["ParentDepartmentID"].AsString(),
                IsParent = reader["IsParent"].AsBool(),
                RestaurantId = reader["RestaurantId"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
